//
//  TJActivationCodeController.m
//  BaoDian
//
//  Created by mac on 2024/7/29.
//

#import "TJActivationCodeController.h"
#import "TJEmptyDataHandler.h"
#import "TJActivationCodeCell.h"
#import "TJUseActivationCodeController.h"
 

@interface TJActivationCodeController ()

@property (nonatomic, strong) NSMutableArray *datas;
@property (nonatomic, strong) TJEmptyDataHandler *emptyDataHandler;

@end

@implementation TJActivationCodeController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationBar.title = @"我的激活码";
    self.navigationBar.rightBarButton = [TJBarButton barButtonWithTitle:@"使用记录" color:UIColor.whiteColor font:UIFontMake(14) target:self action:@selector(didClickRecordButton)];
    
    [self.tableView registerClass:TJActivationCodeCell.class forCellReuseIdentifier:@"TJActivationCodeCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 186;
    self.tableView.contentInset = UIEdgeInsetsMake(16, 0, 0, 0);
    self.tableView.contentOffset = CGPointMake(0, -16);
    self.tableView.backgroundColor = self.view.backgroundColor;
    [self.tableView setSectionIndexColor:UIColor.tj_9B9B9BColor];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight);
        make.left.right.bottom.offset(0);
    }];
    
    _emptyDataHandler = [TJEmptyDataHandler emptyDataHandleWithScrollView:self.tableView configuration:^TJEmptyDataType(TJEmptyDataModel * _Nonnull dataModel) {
        dataModel.text = @"你暂时还没有激活码";
        dataModel.iconName = @"activationCodeEmpty";
        return TJEmptyDataTypeBase;
    }];
    
    [self loadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TJActivationCodeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TJActivationCodeCell" forIndexPath:indexPath];
    TJActiveCodeModel *model = self.datas[indexPath.row];
    cell.model = model;
    TJWeakify(self)
    [cell setDidClickUseButtonBlock:^{
        TJUseActivationCodeController *useActivationCodeController = [[TJUseActivationCodeController alloc] init];
        useActivationCodeController.code = model.activationCode;
        [useActivationCodeController setComplete:^{
            [weakself.datas removeObject:model];
            [weakself.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        }];
        [weakself presentViewController:useActivationCodeController animated:NO completion:nil];
    }];
    return cell;
}

 
- (void)loadData {
    [TJMinePresent loadActiveCodeListWithPageIndex:1 isUse:NO complete:^(NSArray * _Nonnull datas, NSError * _Nullable error) {
        self.emptyDataHandler.canDisplay = YES;
        if (datas.count) {
            [self.datas removeAllObjects];
            [self.datas addObjectsFromArray:datas];
        }
        [self.tableView reloadData];
    }];
}

- (NSMutableArray *)datas {
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}


- (void)didClickRecordButton {
    [self.navigationController pushViewControllerWithName:@"TJActivationCodeUseRecordController"];
}

@end

